package com.intelligentmeterreadingsys.usermanager.activity;

import java.util.List;

import org.achartengine.ChartFactory;
import org.achartengine.chart.BarChart.Type;

import com.intelligentmeterreadingsys.R;
import com.intelligentmeterreadingsys.povo.UseWaterInfoPoVo;
import com.intelligentmeterreadingsys.povo.UserinfoVO;
import com.intelligentmeterreadingsys.usermanager.db.UserInfoDao;
import com.intelligentmeterreadingsys.usermanager.server.AChartEngine;



import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.OnGestureListener;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 用户信息及历史用水量
 * @author king
 *
 */
public class UseWaterInfoActivity extends Activity implements OnClickListener,OnTouchListener,OnGestureListener{
	
	public Button button1;//上一页
	
	public Button button2;//下一页
	
	public Button button3;//历史用水量
	
	public TextView textview1;//用户姓名
	
	public TextView textview2;//用户编号
	public TextView textview3;//用户类型
	public TextView textview4;//用户电话
	public TextView textview5;//用户地址
	public TextView textview6;//描述
	
	//手势
	public 	GestureDetector mGestureDetector; 
	public List<UserinfoVO> list=null;//用户信息
	
	//手势移动量
	 private int verticalMinDistance = 20;  
	 private int minVelocity         = 0;  
	 
	 public int i=0;//当前页数

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.usermanager_userinfo);
		init();
	}
	
	/**
	 * 初始化控件
	 */
    public void init(){
    	button1=(Button)findViewById(R.id.usermanage_lastpage);
    	button2=(Button)findViewById(R.id.usermanager_nextpage);
    	button3=(Button)findViewById(R.id.usermanager_history);
    	button1.setOnClickListener(this);
    	button2.setOnClickListener(this);
    	button3.setOnClickListener(this);
    	
    	textview1=(TextView)findViewById(R.id.usermanager_username);
    	textview2=(TextView)findViewById(R.id.usermanager_usernumber);
    	textview3=(TextView)findViewById(R.id.usermanager_phone);
    	textview4=(TextView)findViewById(R.id.usermanager_type);
    	textview5=(TextView)findViewById(R.id.usermanager_adds);
    	textview6=(TextView)findViewById(R.id.usermanager_info);
    	
    	mGestureDetector=new GestureDetector((OnGestureListener)this);
    	
    	//添加手势
    	RelativeLayout layout=(RelativeLayout)findViewById(R.id.usermanager_layout_one);
		layout.setOnTouchListener(this);
		layout.setLongClickable(true);
		
    	Intent intent=getIntent();
    	Bundle bundle=intent.getExtras();
    	boolean b=bundle.getBoolean("key");
    	
    	UserInfoDao dao=new UserInfoDao(this);
    	if(b){
    		this.list=dao.queryUserInfo(bundle.getString("username"), bundle.getString("adds"),
    				bundle.getString("phone"),bundle.getString("number"), bundle.getString("type"));
    	}else{
    		this.list=dao.queryUserInfo(null, null, null, null, null);
    	}
    	dao.close();
    	addDate();
    }

	@Override
	public void onClick(View v) {
		if(v.getId()==button1.getId()){
			if(list!=null){
				i--;
				filp();
			}						
		}else if(button2.getId()==v.getId()){
			if(list!=null){
				i++;
				filp();
			}		
		}else if(button3.getId()==v.getId()){
			AChartEngine ace=new AChartEngine();
			if(list==null|list.size()<1){
				Toast.makeText(UseWaterInfoActivity.this, "没有用户信息不能操作！", Toast.LENGTH_LONG).show();
				return ;
			}
			String usernumber=list.get(i).getNumber();
			UserInfoDao dao=new UserInfoDao(this);
			List<UseWaterInfoPoVo> lists=dao.queryUseWater(usernumber);
			
			dao.close();
			if(lists==null|lists.size()<1){
				
				Toast.makeText(UseWaterInfoActivity.this, "没有找到用户或应该用户没历史用水信息！", Toast.LENGTH_LONG).show();
				return ;
			}
			 Intent intent = ChartFactory.getBarChartIntent(UseWaterInfoActivity.this, ace.getBarDataset(lists), ace.getBarRenderer(lists), Type.DEFAULT);
			  startActivity(intent);
		}
	}
	
	/**
	 * 给界面添加数据
	 */
	public void addDate(){
		if(list!=null&&list.size()>0){
			UserinfoVO user=list.get(i);
			textview1.setText(user.getName());
			textview2.setText(user.getNumber());
			textview3.setText(user.getType());
			textview4.setText(user.getPhone());
			textview5.setText(user.getAdds());
			textview6.setText("共查到用户数："+list.size()+" 当前第"+(i+1)+"位");
			
			
		}
		
	}
	/**
	 * 翻页
	 */
	public void filp(){
		if(i<0){
			i=0;
			Toast.makeText(UseWaterInfoActivity.this, "没有前面的数据了！", Toast.LENGTH_LONG).show();
		}else if(i>(list.size()-1)){
			i=list.size()-1;
			Toast.makeText(UseWaterInfoActivity.this, "没有后面的数据了！", Toast.LENGTH_LONG).show();
		}else{
			addDate();
		}
	}

	@Override
	public boolean onDown(MotionEvent arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {

		if (e1.getX() - e2.getX() > verticalMinDistance && Math.abs(velocityX) > minVelocity) {  
			if(list!=null){
				 i--;
				 filp();
			}
			
			 Toast.makeText(this, "向左手势", Toast.LENGTH_SHORT).show();  
		}else{
			if(list!=null){
				 i++;
				 filp();
			}
			 Toast.makeText(this, "向右手势", Toast.LENGTH_SHORT).show();  
		}
		return false;
	
	}

	@Override
	public void onLongPress(MotionEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float velocityX,
			float distanceY) {
				return false;
		
	}

	@Override
	public void onShowPress(MotionEvent e) {
		
		
	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		
		return false;
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
	
		return mGestureDetector.onTouchEvent(event);
	}
	
	
}
